package com.dao.impl;

import com.bean.History;
import com.dao.BaseDao;
import com.dao.HistoryDao;

import java.util.List;

public class HistoryDaoImpl extends BaseDao implements HistoryDao {
    /**
     * 历史记录 通过用户的id和视频的id查找
     *
     */
//    @Override
//    public History querHistoryViedoIdAndUserId() {
//       String sql = "Select  DISTINCT viedo.vname,history.startTime FROM `history` LEFT JOIN `user` on user.id= history.userId LEFT JOIN viedo on viedo.id = history.viedoId WHERE `viedoId`=? and `userId`=?";
//     return queryForOne(History.class,sql);
//    }
//
//    @Override
//    public List<History> queryHistory() {
//        return null;
//    }

    /**
     * 前台根据用户Id查找历史记录
     * @param userId
     * @return
     */
    @Override
    public List<History> querHistoryByUserId(Integer userId) {
        String sql= "select * from history where userId = ?";
        return queryForList(History.class,sql,userId);
    }

    /**
     * 前台根据用户id和视频id查找历史记录
     * @param viedoId
     * @param userId
     * @return
     */
    @Override
    public History frontFindByUidAndVideoId(Integer viedoId, Integer userId) {
        String sql ="select * from history where userId =?";
        return queryForOne(History.class,sql,viedoId,userId);
    }

    /**
     * 前台更新历史记录
     * @param history
     * @return
     */
    @Override
    public int frontUpdate(History history) {
String sql= "update history set startTime = ?, watchTime = ? where userId =? and videoId = ?";
return update(sql,history.getStartTime(),history.getWatchTime(),history.getUserId(),history.getViedoId());
    }

    /**
     * 前台保存历史记录
     * @param userId
     * @param viedoId
     * @param watchTime
     * @return
     */
    @Override
    public int frontSave(Integer userId, Integer viedoId, Double watchTime) {
        String sql= "insert into history (userId, videoId, startTime, watchTime) values (?,?,?,?)";
        return update(sql,userId,viedoId,watchTime);

    }
}
